package util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import user.vo.User;

/**
 * 登录权限过滤,未登录不能访问
 * @author ljldn
 *
 */
public class PowerAccessFilter implements Filter{
	private FilterConfig fc;
	
	@Override
	public void init(FilterConfig filterconfig) throws ServletException {
		// TODO Auto-generated method stub
		fc = filterconfig;
	}

	@Override
	public void doFilter(ServletRequest servletrequest,
			ServletResponse servletresponse, FilterChain filterchain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest) servletrequest;
		HttpServletResponse resp = (HttpServletResponse) servletresponse;
		String uri = req.getRequestURI();//  /mvc/
		String path = req.getContextPath(); //   /mvc
		//System.out.println("uri:"+uri);
		//System.out.println("path:"+path);
		User user = (User) req.getSession().getAttribute("user");
		if(null == user &&  !"/mvc/".equals(uri) && !"/mvc/login.jsp".equals(uri) && !"/mvc/loginSer".equals(uri)){
			//System.out.println("未登录");
			//resp.sendRedirect("login.jsp");
			//return;
		}
		filterchain.doFilter(servletrequest, servletresponse);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	

}
